home *** CD-ROM | disk | FTP | other *** search
/ Night Owl 6 / Night Owl's Shareware - PDSI-006 - Night Owl Corp (1990).iso / 038a / ike.zip / OPENFILE.C < prev    next >
C/C++ Source or Header  |  1991-01-08  |  10KB  |  315 lines

  1. /*===========================================================================*/
  2. /*                                                                           */
  3. /* File    : OPENFILE.C                                                      */
  4. /*                                                                           */
  5. /* Purpose : Provides the File/Open and File/Save dialog boxes.              */
  6. /*                                                                           */
  7. /* History : This came from the MS Win 3.0 SDK                               */
  8. /*                                                                           */
  9. /*===========================================================================*/
  10.  
  11. #include <stdlib.h>
  12. #include <ctype.h>
  13. #include <dos.h>
  14. #include <fcntl.h>
  15. #include <io.h>
  16. #include <sys\types.h>
  17. #include <sys\stat.h>
  18. #include <string.h>
  19. #include <windows.h>
  20. #include "ico.h"
  21.  
  22. #define ID_EDIT        100
  23. #define ID_PATH        101
  24. #define ID_LISTBOX     102
  25. #define ID_DIRLISTBOX  103
  26.  
  27. void SeparateFile(LPSTR, LPSTR, LPSTR);
  28. void UpdateListBox(HWND);
  29. void ChangeDefExt(PSTR, PSTR);
  30. void UpdateSaveasListBox(HWND);
  31. int  ICORead(HWND, char *);
  32.  
  33.  
  34.  
  35. char DefPath[128];
  36. char DefSpec[13] = "*.ico";
  37. char DefExt[13] = ".ico";
  38.  
  39.  
  40. BOOL FAR PASCAL OpenFileDlgProc(hDlg, message, wParam, lParam)
  41.   HWND hDlg;
  42.   unsigned message;
  43.   WORD wParam;
  44.   LONG lParam;
  45. {
  46.   char szBuf[128];
  47.   char OpenName[128];
  48.  
  49.   switch (message)
  50.   {
  51.     case WM_INITDIALOG:                          /* message: initialize    */
  52.       strcpy(DefSpec, "*.ico");
  53.       strcpy(DefExt,  "*.ico");
  54.       UpdateListBox(hDlg);
  55.       SetDlgItemText(hDlg, ID_EDIT, DefSpec);
  56.       SendDlgItemMessage(hDlg,                   /* dialog handle          */
  57.                          ID_EDIT,                /* where to send message  */
  58.                          EM_SETSEL,              /* select characters      */
  59.                          NULL,                   /* additional information */
  60.                          MAKELONG(0, 0x7FFF));   /* entire contents        */
  61.       SetFocus(GetDlgItem(hDlg, ID_EDIT));
  62.       return FALSE;   /* Indicates the focus is set to a control */
  63.  
  64.  
  65.     case WM_COMMAND:
  66.       switch (wParam)
  67.       {
  68.         case ID_LISTBOX:
  69.            switch (HIWORD(lParam))
  70.            {
  71.              case LBN_SELCHANGE:
  72.                /* If item is a directory name, append "*.*" */
  73.                if (DlgDirSelect(hDlg, szBuf, ID_LISTBOX)) 
  74.                  strcat(szBuf, DefSpec);
  75.                SetDlgItemText(hDlg, ID_EDIT, szBuf);
  76.                SendDlgItemMessage(hDlg, ID_EDIT, EM_SETSEL, NULL,
  77.                                                  MAKELONG(0, 0x7FFF));
  78.                break;
  79.  
  80.              case LBN_DBLCLK:
  81.                goto openfile;
  82.            }
  83.            return TRUE;
  84.  
  85.         case ID_DIRLISTBOX:
  86.            switch (HIWORD(lParam))
  87.            {
  88.              case LBN_SELCHANGE:
  89.                /* If item is a directory name, append "*.*" */
  90.                if (DlgDirSelect(hDlg, szBuf, ID_DIRLISTBOX)) 
  91.                  strcat(szBuf, DefSpec);
  92.                SetDlgItemText(hDlg, ID_EDIT, szBuf);
  93.                SendDlgItemMessage(hDlg, ID_EDIT, EM_SETSEL, NULL,
  94.                                                  MAKELONG(0, 0x7FFF));
  95.                break;
  96.  
  97.              case LBN_DBLCLK:
  98.                goto openfile;
  99.                break;
  100.            }
  101.            return TRUE;
  102.  
  103.          case IDOK:
  104. openfile:
  105.            GetDlgItemText(hDlg, ID_EDIT, OpenName, 128);
  106.            if (strchr(OpenName, '*') || strchr(OpenName, '?'))
  107.            {
  108.              SeparateFile(szBuf, DefSpec, OpenName);
  109.              if (szBuf[0])
  110.                strcpy(DefPath, szBuf);
  111.              ChangeDefExt(DefExt, DefSpec);
  112.              UpdateListBox(hDlg);
  113.              return TRUE;
  114.            }
  115.            if (!OpenName[0])
  116.            {
  117.              MessageBox(hDlg, "No filename specified.", "Error",
  118.                                                         MB_OK | MB_ICONHAND);
  119.              return TRUE;
  120.            }
  121.  
  122.            /*
  123.               Add the extension to the file name if it doesn't have
  124.               an extension already
  125.            */
  126.            if (strchr(OpenName, '.') == NULL)
  127.              strcat(OpenName, DefExt);
  128.  
  129.            ICORead(hWndEdit, OpenName);
  130.  
  131.            EndDialog(hDlg, IDOK);
  132.            return TRUE;
  133.  
  134.          case IDCANCEL:
  135.            EndDialog(hDlg, IDCANCEL);
  136.            return TRUE;
  137.       }
  138.     }
  139.     return FALSE;
  140. }
  141.  
  142.  
  143. void UpdateListBox(hDlg)
  144.   HWND hDlg;
  145. {
  146.   char str[80];
  147.  
  148.   strcpy(str, DefPath);
  149.   strcat(str, DefSpec);
  150.   DlgDirList(hDlg, str, ID_LISTBOX, ID_PATH, 0x0000);
  151.   DlgDirList(hDlg, "*.*", ID_DIRLISTBOX, 0, 0xC010);
  152.   SetDlgItemText(hDlg, ID_EDIT, DefSpec);
  153.   SendDlgItemMessage(hDlg, ID_EDIT, EM_SETSEL, NULL, MAKELONG(0, 0x7FFF));
  154. }
  155.  
  156.  
  157. void ChangeDefExt(Ext, Name)
  158.   PSTR Ext, Name;
  159. {
  160.   PSTR pTptr;
  161.  
  162.   if ((pTptr = strchr(Name, '.')) != NULL)
  163.     if (!strchr(pTptr, '*') && !strchr(pTptr, '?'))
  164.       strcpy(Ext, pTptr);
  165. }
  166.  
  167.  
  168. void SeparateFile(lpDestPath, lpDestFileName, lpSrcFileName)
  169.   LPSTR lpDestPath, lpDestFileName, lpSrcFileName;
  170. {
  171.   LPSTR lpTmp;
  172.   char  cTmp;
  173.  
  174.   /*
  175.      Scan backwards until we find a '\' or a drive designator
  176.   */
  177.   lpTmp = lpSrcFileName + (long) lstrlen(lpSrcFileName);
  178.   while (*lpTmp != ':' && *lpTmp != '\\' && lpTmp > lpSrcFileName)
  179.     lpTmp = AnsiPrev(lpSrcFileName, lpTmp);
  180.  
  181.   if (*lpTmp != ':' && *lpTmp != '\\')
  182.   {
  183.     lstrcpy(lpDestFileName, lpSrcFileName);
  184.     lpDestPath[0] = '\0';
  185.     return;
  186.   }
  187.  
  188.   /*
  189.     Copy the actual file name or file spec into pDestFileName
  190.   */
  191.   lstrcpy(lpDestFileName, lpTmp + 1);
  192.   cTmp = *(lpTmp + 1);
  193.  
  194.   /*
  195.     Copy the root part of the path into lpDestPath
  196.   */
  197.   lstrcpy(lpDestPath, lpSrcFileName);
  198.   *(lpTmp + 1) = cTmp;
  199.  
  200.   lpDestPath[(lpTmp - lpSrcFileName) + 1] = '\0';
  201. }
  202.  
  203.  
  204.  
  205. BOOL FAR PASCAL SaveAsDlgProc(hDlg, message, wParam, lParam)
  206.   HWND hDlg;
  207.   unsigned message;
  208.   WORD wParam;
  209.   LONG lParam;
  210. {
  211.   char szBuf[128];
  212.   char OpenName[128];
  213.   int  id;
  214.   char *pDot;
  215.  
  216.  
  217.   switch (message)
  218.   {
  219.     case WM_INITDIALOG:                          /* message: initialize    */
  220.       DlgDirList(hDlg, "*.*", ID_DIRLISTBOX, ID_PATH, 0xC010);
  221.       SetDlgItemText(hDlg, ID_EDIT, DefSpec);
  222.       SendDlgItemMessage(hDlg,                   /* dialog handle          */
  223.                          ID_EDIT,                /* where to send message  */
  224.                          EM_SETSEL,              /* select characters      */
  225.                          NULL,                   /* additional information */
  226.                          MAKELONG(0, 0x7FFF));   /* entire contents        */
  227.       SetFocus(GetDlgItem(hDlg, ID_EDIT));
  228.       return FALSE;   /* Indicates the focus is set to a control */
  229.  
  230.  
  231.     case WM_COMMAND:
  232.       switch (wParam)
  233.       {
  234.         case ID_DIRLISTBOX:
  235.            switch (HIWORD(lParam))
  236.            {
  237.              case LBN_SELCHANGE:
  238.                if (DlgDirSelect(hDlg, szBuf, ID_DIRLISTBOX)) 
  239.                  strcat(szBuf, DefSpec);
  240.                SetDlgItemText(hDlg, ID_EDIT, szBuf);
  241.                SendDlgItemMessage(hDlg, ID_EDIT, EM_SETSEL, NULL,
  242.                                                  MAKELONG(0, 0x7FFF));
  243.                break;
  244.              case LBN_DBLCLK:
  245.                GetDlgItemText(hDlg, ID_EDIT, OpenName, 128);
  246.                SeparateFile(szBuf, DefSpec, OpenName);
  247.                if (szBuf[0])
  248.                  strcpy(DefPath, szBuf);
  249.                strcat(DefPath, DefSpec);
  250.                DlgDirList(hDlg, szBuf, ID_DIRLISTBOX, ID_PATH, 0xC010);
  251.                break;
  252.            }
  253.            return TRUE;
  254.  
  255.          case IDOK:
  256. openfile:
  257.            GetDlgItemText(hDlg, ID_EDIT, OpenName, 128);
  258.            if (strchr(OpenName, '*') || strchr(OpenName, '?'))
  259.            {
  260.              MessageBox(hDlg, "Illegal filename specified.", "Error",
  261.                                                         MB_OK | MB_ICONHAND);
  262.              return TRUE;
  263.            }
  264.            if (!OpenName[0])
  265.            {
  266.              MessageBox(hDlg, "No filename specified.", "Error",
  267.                                                         MB_OK | MB_ICONHAND);
  268.              return TRUE;
  269.            }
  270.  
  271.            ICOWrite(OpenName, IconInfo.hBitmapBits);
  272.            strcpy(IconInfo.szFilename, OpenName);
  273.            EndDialog(hDlg, IDOK);
  274.            return TRUE;
  275.  
  276.          case IDCANCEL:
  277.            EndDialog(hDlg, IDCANCEL);
  278.            return TRUE;
  279.       }
  280.       return TRUE;
  281.   }
  282.  
  283.   return FALSE;
  284. }
  285.  
  286.  
  287. /****************************************************************************/
  288. /*                                                                          */
  289. /* Function :  About                                                        */
  290. /*                                                                          */
  291. /* Purpose  :  Dialog box proc for the about box.                           */
  292. /*                                                                          */
  293. /* Returns  : TRUE if we processed the message, FALSE if not.               */
  294. /*                                                                          */
  295. /****************************************************************************/
  296. BOOL FAR PASCAL About(HWND hDlg, unsigned message, WORD wParam, LONG lParam)
  297. {
  298.   switch (message)
  299.   {
  300.     case WM_INITDIALOG:
  301.       return TRUE;
  302.  
  303.     case WM_COMMAND:
  304.       if (wParam == IDOK)
  305.       {
  306.         EndDialog(hDlg, TRUE);
  307.         return TRUE;
  308.       }
  309.       break;
  310.   }
  311.   return FALSE;
  312. }
  313.  
  314.  
  315.